package com.book.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.book.dto.PostDto;
import com.book.entity.Post;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PostDao extends BaseMapper<Post> {

     @Select("SELECT \n" +
             "    a.id,a.title,a.content,a.user_id as userId,\n" +
             "    (select head from user where id=a.user_id and del_flag=0) as head,\n" +
             "    (select nick_name from user where id=a.user_id and del_flag=0) as nickName,\n" +
             "    a.posting_time,\n" +
             "    (select count(b.post_id) from apply b where b.post_id=a.id and b.del_flag=0) as totalApply\n" +
             "FROM post a\n" +
             "where\n" +
             "        (a.title like CONCAT('%', #{message}, '%') \n" +
             "    or\n" +
             "        a.content like CONCAT('%', #{message}, '%') ) and a.del_flag=0\n" +
             "order by a.posting_time desc")
    List<PostDto> getList(String message);

    /**
     * 这里是根据帖子的id获取这个帖子的详情
     * @param id
     * @return
     */
    @Select("SELECT \n" +
            "    a.id,a.title,a.content,a.user_id as userId,\n" +
            "    (select head from user where id=a.user_id) as head,\n" +
            "    (select nick_name from user where id=a.user_id) as nickName,\n" +
            "    a.posting_time\n" +
            "FROM post a\n" +
            "where\n" +
            "    a.id=#{id}\n"+
            "order by a.posting_time desc")
    PostDto getOne(int id);
}